package com.millstein.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.millstein.eduservice.entity.EduChapter;
import com.millstein.eduservice.entity.EduCourse;
import com.millstein.eduservice.entity.EduCourseDescription;
import com.millstein.eduservice.entity.EduVideo;
import com.millstein.eduservice.entity.vo.CourseInfoVo;
import com.millstein.eduservice.entity.vo.CoursePublishVo;
import com.millstein.eduservice.mapper.EduCourseMapper;
import com.millstein.eduservice.service.EduChapterService;
import com.millstein.eduservice.service.EduCourseDescriptionService;
import com.millstein.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.millstein.eduservice.service.EduVideoService;
import com.millstein.servicebase.handler.exceptionhandler.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-07-15
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Autowired
    private EduCourseDescriptionService eduCourseDescriptionService;

    @Autowired
    private EduVideoService eduVideoService;

    @Autowired
    private EduChapterService eduChapterService;

    /**
     * 添加课程
     * @param courseInfoVo
     */
    @Override
    @Transactional
    public String saveCourseInfo(CourseInfoVo courseInfoVo) {
        // 1.向edu_course表中添加课程的基本信息
        EduCourse course = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo, course);
        boolean result = this.save(course);
        if (!result) {
            throw new GuliException(20001, "添加课程失败");
        }

        // 2.向edu_course_description表中添加课程的描述信息
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setDescription(courseInfoVo.getDescription());
        eduCourseDescription.setId(course.getId());
        result = eduCourseDescriptionService.save(eduCourseDescription);
        if (!result) {
            throw new GuliException(20001, "添加课程失败");
        }

        return course.getId();
    }

    @Override
    public CourseInfoVo getCourseInfo(String courseId) {
        EduCourse course = this.getById(courseId);
        EduCourseDescription courseDescription = eduCourseDescriptionService.getById(courseId);

        CourseInfoVo vo = new CourseInfoVo();
        BeanUtils.copyProperties(course, vo);
        BeanUtils.copyProperties(courseDescription, vo);
        return vo;
    }

    @Override
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {
        EduCourse course = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo, course);
        boolean result = this.updateById(course);
        if (!result) {
            throw new GuliException(20001, "更新课程信息失败");
        }

        EduCourseDescription description = new EduCourseDescription();
        BeanUtils.copyProperties(courseInfoVo, description);
        result = eduCourseDescriptionService.updateById(description);
        if (!result) {
            throw new GuliException(20001, "更新课程信息失败");
        }
    }

    @Override
    public CoursePublishVo getPublishCourseInfo(String courseId) {
        return baseMapper.getPublishCourseInfo(courseId);
    }

    @Override
    @Transactional
    public void deleteCourse(String courseId) {
        // 1.删小节
        QueryWrapper<EduVideo> vWrapper = new QueryWrapper<>();
        vWrapper.eq("course_id", courseId);
        boolean result = eduVideoService.remove(vWrapper);
        if (!result) {
            throw new GuliException(20001, "删除课程信息失败");
        }

        // 2.删章节
        QueryWrapper<EduChapter> cWrapper = new QueryWrapper<>();
        cWrapper.eq("course_id", courseId);
        result = eduChapterService.remove(cWrapper);
        if (!result) {
            throw new GuliException(20001, "删除课程信息失败");
        }
        // 3.删描述
        result = eduCourseDescriptionService.removeById(courseId);
        if (!result) {
            throw new GuliException(20001, "删除课程信息失败");
        }

        // 4.删课程
        result = this.removeById(courseId);
        if (!result) {
            throw new GuliException(20001, "删除课程信息失败");
        }
    }
}
